package com.yys.huarongdao.entity;

import android.util.Log;
import android.widget.Toast;

import com.yys.huarongdao.R;

import java.util.ArrayList;
import java.util.Collections;

public  class ArrayIcon {


    //图片资源
    public final static int []icons= new int[]{
            R.drawable.number_1,R.drawable.number_2,R.drawable.number_3,R.drawable.number_4,R.drawable.number_5,
            R.drawable.number_6,R.drawable.number_7,R.drawable.number_8,R.drawable.number_9,R.drawable.number_10,
            R.drawable.number_11,R.drawable.number_12,R.drawable.number_13,R.drawable.number_14,R.drawable.number_15,
            R.drawable.number_16,R.drawable.number_17,R.drawable.number_18,R.drawable.number_19,R.drawable.number_20,
            R.drawable.number_21,R.drawable.number_22,R.drawable.number_23,R.drawable.number_24,R.drawable.number_25,
    };

    /**
     *求数字序列的逆序数
     * @return 返回数字序列中的逆序数
     */
    private static int getInversionCount(ArrayList<Integer> numberArrayList){
        int count=0;
        int maxNum=numberArrayList.size()-1;
        for(int i=0;i<numberArrayList.size()-1;i++){
            if(numberArrayList.get(i)==maxNum)
                continue;
            for(int j=i+1;j<numberArrayList.size();j++){
                if(numberArrayList.get(j)==maxNum)
                    continue;
                if(numberArrayList.get(i)>numberArrayList.get(j)){
                    count++;
                }
            }
        }
        return count;
    }

    /**
     *判断数字序列是否有解
     */
    private static boolean isSoluble(ArrayList<Integer> numberArrayList){
        int length=numberArrayList.size();
        int colCount=(int)Math.sqrt(length);
        //列数colCount为奇数时，逆序数为偶数，必然有解。
        if(colCount%2!=0&&getInversionCount(numberArrayList)%2==0){
//            Log.d("isSoluble","length="+length+",colCount="+colCount+",逆序数"+getInversionCount(numberArrayList));
            return true;
        }
        int currentBlankPosition = -1;
        for(int i=0;i<length;i++){
            if(numberArrayList.get(i)==length-1){
                currentBlankPosition=i;
            }
        }
        if(currentBlankPosition==-1)
            return false;
        int startBlankPosition=length-1;
        //列数colCount为偶数时，（逆序数）与（当前空格所在的行数和初始空格所在的行数之差）的和为偶数时必然有解。
        if(colCount%2==0&&(getInversionCount(numberArrayList)+currentBlankPosition-startBlankPosition)%2==0)
            return true;
        return false;
    }

    /**
     * 生成有解的数字序列
     * 序列中的逆序数为偶数必定有解
     * 1、生成随机序列
     * 2、判断序列中的逆序数是否为偶数
     * @param length 序列长度
     */
    public static ArrayList<Integer> getNumberArrayList(int length){
        ArrayList<Integer> numberArrayList;
        numberArrayList = new ArrayList<Integer>();
        for(int i=0;i<length;i++){
            numberArrayList.add(Integer.valueOf(i));
        }
        do{
            Collections.shuffle(numberArrayList);
        }while (!isSoluble(numberArrayList));
        return numberArrayList;
    }

    public static ArrayList<Icon> getIconArrayList(ArrayList<Integer> numberArrayList){

        if(numberArrayList==null||numberArrayList.isEmpty())
            return null;
        ArrayList<Icon> iconArrayList;
        iconArrayList=new ArrayList<Icon>();

        for(int i=0; i<numberArrayList.size();i++){
            //添加空白图片
            if(numberArrayList.get(i)==numberArrayList.size()-1){
                iconArrayList.add(new Icon(R.drawable.blank,"图片：空白"));
            }else {
                iconArrayList.add(new Icon(icons[numberArrayList.get(i)],"图片："+ (i+1)));
            }
        }

        return iconArrayList;
    }

    public static int swapItem(ArrayList<Integer> numberArrayList,ArrayList<Icon> iconArrayList,int position,int blankPosition){
        int lastBlankPosition=-1;
        int colCount=(int) Math.sqrt(iconArrayList.size());

        boolean isOnLeft=position==blankPosition-1&&blankPosition%colCount!=0;
        boolean isOnRight=position==blankPosition+1&&(blankPosition+1)%colCount!=0;
        boolean isAbove=position==blankPosition-colCount;
        boolean isBottom=position==blankPosition+colCount;

        if(isOnLeft||isOnRight||isAbove||isBottom) {
            //图片数组交换数据
            Icon tem=iconArrayList.get(position);
            iconArrayList.set(position,iconArrayList.get(blankPosition));
            iconArrayList.set(blankPosition,tem);
            //数字序列也做交换，方便后面判断胜负
            int temNum=numberArrayList.get(position);
            numberArrayList.set(position,numberArrayList.get(blankPosition));
            numberArrayList.set(blankPosition,temNum);
            lastBlankPosition=blankPosition;
        }
        return lastBlankPosition;
    }

    public static boolean isSuccess(ArrayList<Integer> numberArrayList){
        boolean result=true;
        for(int i=0;i<numberArrayList.size()-1;i++){
            if(numberArrayList.get(i)>numberArrayList.get(i+1)){
                result=false;
                break;
            }
        }
        return result;
    }

    public static void goBack(ArrayList<Integer> numberArrayList,ArrayList<Icon> iconArrayList,int currentBlankPosition,int lastBlankPosition){
        //图片数组交换数据
        Icon tem=iconArrayList.get(currentBlankPosition);
        iconArrayList.set(currentBlankPosition,iconArrayList.get(lastBlankPosition));
        iconArrayList.set(lastBlankPosition,tem);
        //数字序列也做交换，方便后面判断胜负
        int temNum=numberArrayList.get(currentBlankPosition);
        numberArrayList.set(currentBlankPosition,numberArrayList.get(lastBlankPosition));
        numberArrayList.set(lastBlankPosition,temNum);
    }
}
